import 'dart:convert';

import 'package:fish_redux/fish_redux.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:oktoast/oktoast.dart';
import 'package:school_safe_teacher/Home/entity/homeClassEntity.dart';
import 'package:school_safe_teacher/base/base_entity.dart';
import 'package:school_safe_teacher/me/entity/userEntity.dart';
import 'package:school_safe_teacher/base/entity_factory.dart';
import 'package:school_safe_teacher/constans/all_info.dart';
import 'package:school_safe_teacher/constans/constants.dart';
import 'package:school_safe_teacher/network/dio_api.dart';
import 'package:school_safe_teacher/network/http_api.dart';
import 'package:school_safe_teacher/utils/sp_utils.dart';

import 'action.dart';
import 'entity/news_entity.dart';
import 'state.dart';

Effect<HomePageState> buildEffect() {
  return combineEffects(<Object, Effect<HomePageState>>{
    HomePageAction.onRefresh: _onRefresh,
    HomePageAction.onLoad: _onLoad,
    Lifecycle.initState: _init,
    HomePageAction.onChooseClassSuccess: _onChooseClassSuccess,
  });
}

void _onRefresh(Action action, Context<HomePageState> ctx) {
  _request(ctx, 1);
}

void _init(Action action, Context<HomePageState> ctx) {
  if (AllInfo.instance.token.isNotEmpty) {
    _homeClassRequest(ctx);
  }
}

void _onLoad(Action action, Context<HomePageState> ctx) {
  _request(ctx, action.payload);
}
void _onChooseClassSuccess(Action action, Context<HomePageState> ctx) {
  _requestsetDefaultClass(ctx, action.payload);
}
Future<void> _requestsetDefaultClass(Context<HomePageState> ctx, String classid) async {
  Map<String, String> params = Map();
  params["class_id"] = classid;

  DioApi.instance.asyncRequestNetwork<BaseEntity>(
      Method.post, HttpApi.HOME_SET_DEFAULTCLASS, ctx.context,params: params, onStart: () {},
      onSuccess:(dynamic t){
        _homeClassRequest(ctx);
      }, onError: (String msg) {
    showToast(msg);
  }, onDone: () {
    ctx.dispatch(HomePageActionCreator.onComplete());
  }, isList: false);
}

Future<void> _request(Context<HomePageState> ctx, int page) async {
  Map<String, String> params = Map();
  params["page_size"] = Constants.PAGE_SIZE.toString();
  params["page"] = page.toString();
  DioApi.instance.asyncRequestNetwork<NewsEntity>(
      Method.post, HttpApi.NEWS_LIST, ctx.context,params: params, onStart: () {},
      onSuccessList: (List<dynamic> list) async {
    List<NewsEntity> newsList = [];
    list.forEach((element) {
      newsList.add(EntityFactory.generateOBJ(element));
    });
    ctx.dispatch(HomePageActionCreator.onSuccess(newsList));
  }, onError: (String msg) {
    showToast(msg);
  }, onDone: () {
    ctx.dispatch(HomePageActionCreator.onComplete());
  }, isList: true);
}
Future<void> _homeClassRequest(Context<HomePageState> ctx) async {
  Map<String, String> params = Map();
  DioApi.instance.asyncRequestNetwork<NewsEntity>(
      Method.post, HttpApi.HOME_LIST,ctx.context, params: params, onStart: () {
        EasyLoading.show();
      },
      onSuccess:(dynamic t){
        homeClassEntity entity= EntityFactory.generateOBJ(t);
        entity.classList.forEach((element) {
          if(element.isDefault==1){
            ctx.state..defaultClass = element
                ..classname = element.className
            ..classList = entity.classList;

            SpUtil.setStringItem(Constants.classID, element.classId.toString());
          }
        });
        ctx.dispatch(HomePageActionCreator.onAction());
      }, onError: (String msg) {
    showToast(msg);
  }, onDone: () {
        EasyLoading.dismiss();
  }, isList: false);
}
